<?php

class MySqlConnect
{
    public function GetConn()
    {
    	//内网
//      $con = mysqli_connect("rdstbo42960uh0vfibx5.mysql.rds.aliyuncs.com", "jkroot", "Jkera2016", "lieren");
		//外网
        $con = mysqli_connect("jkera2016.mysql.rds.aliyuncs.com", "jkroot", "Jkera2016", "vivi");
//      $con = mysqli_connect("127.0.0.1", "root", "Jkera2016", "test");
        $con->query("set names utf8");
        return $con;
    }

    public function ExecuteData($sql)
    {
        $con = $this->GetConn();
        if ($con->query($sql)) {
        		mysqli_close($con);
            return 1;
        }else{
       	 	mysqli_close($con);
            return 0;
        }
    }
	


    
	public function GetDataSetJson($sql)
    {
        $con = $this->GetConn();
        $result = $con->query($sql);
        if ($result) {
            $users = array();
            $i = 0;
            $fields = $result->fetch_fields();
            $length = count($fields);
            $count = $result->num_rows;
            $json = "";
            $json .= "{\"data1\":[";
            while ($row = $result->fetch_assoc()) {
                $json .= "{";
                for ($i = 0; $i <= $length - 1; $i++) {
                    $ziduan = $fields[$i]->name;
                    $json .= "\"" . $ziduan . "\":\"" . $row[$ziduan] . "\"";
                    if ($i < $length - 1) {
                        $json .= ",";
                    }
                }
                $json .= "}";
                if ($count > 1) {
                    $count--;
                    $json .= ",";
                }
            }
            $json .= "]}";
			mysqli_close($con);
            return $json;
            
        }
    }
	
	public function GetDataSetJsonPHP($sql)
    {
        $con = $this->GetConn();
        $result = $con->query($sql);
        if ($result) {
            $users = array();
            $i = 0;
            $fields = $result->fetch_fields();
            $length = count($fields);
            $count = $result->num_rows;
            $json = "";
            $json .= "[";
            while ($row = $result->fetch_assoc()) {
                $json .= "{";
                for ($i = 0; $i <= $length - 1; $i++) {
                    $ziduan = $fields[$i]->name;
                    $json .= "\"" . $ziduan . "\":\"" . $row[$ziduan] . "\"";
                    if ($i < $length - 1) {
                        $json .= ",";
                    }
                }
                $json .= "}";
                if ($count > 1) {
                    $count--;
                    $json .= ",";
                }
            }
            $json .= "]";
			mysqli_close($con);
            return $json;
            
        }
    }

//	public function choujiang($mysqlconnect,$openid){
//		$nowtime = date('y-m-d->h:i:s',time());
//		$gailv = rand(0, 19051);
//		$jiangpin=0;
//		$jiangpinstr="";
//		$jiangpininfo = $mysqlconnect->GetDataSetJsonPHP("select * from lieren_jiangpin");
//		$shuzu = json_decode($jiangpininfo);
//	//	echo $shuzu[0]->id;
//		$shengyu_jp1 = $shuzu[1]->shengyushu;//电影票		1000
//		$shengyu_jp2 = $shuzu[2]->shengyushu;//enjoy优惠券	8000
//		$shengyu_jp3 = $shuzu[3]->shengyushu;//围巾			40
//		$shengyu_jp4 = $shuzu[4]->shengyushu;//雨伞			10
//		$shengyu_jp5 = $shuzu[5]->shengyushu;//旅游			1
//		$shengyu_jp6 = $shuzu[6]->shengyushu;//西十区		10000     
//		
//		$zhongguojianginfo =  $mysqlconnect->GetDataSetJsonPHP("select * from lieren_users where openid='$openid'");
//		$userinfo = json_decode($zhongguojianginfo);
//		//1是中奖，2是未中奖
//		$user_jp1=$userinfo[0]->jiangpin1;
//		$user_jp2=$userinfo[0]->jiangpin2;
//		$user_jp3=$userinfo[0]->jiangpin3;
//		$user_jp4=$userinfo[0]->jiangpin4;
//		$user_jp5=$userinfo[0]->jiangpin5;
//		$user_jp6=$userinfo[0]->jiangpin6;
//		
//		$jieguo = 0;
//		//11恭喜抽中电影票，12已经中过电影票
//		//21恭喜抽中enjoy优惠券，22已经中过enjoy优惠券
//		//31恭喜抽中围巾，32已经中过围巾
//		//41恭喜抽中雨伞，42已经中过雨伞
//		//51恭喜抽中旅游，52已经中过旅游
//		//61恭喜抽中西十区，62已经中过西十区
//		if($openid=="oONuCv6hrgT-jNbqJRL5fFHf7IfQ")
//		{
////			$gailv = 10999;	
//		}
//
//		if($gailv>100000 && $gailv<=110000)
//		{
//			if($user_jp1 == "1"){//中过该奖品
//				$jieguo=12;
//			}
//			if($user_jp1 == "2"){//此次中该奖品
//				if($shengyu_jp1>0)
//				{
//					////////////////////////////////改变微票的信息
////					$querystr = "select * from lieren_weipiao where lingquren='$openid'";
////					$info =json_decode($mysqlconnect->GetDataSetJsonPHP($querystr)) ;
////					$count = count($info);
////					if($count==0)//当当前用户微票码为0时才增加
////					{
////						//先找到还没有被领取的一张微票号的id
////						$queryid = $mysqlconnect->GetDataSetJsonPHP("select * from lieren_weipiao where lingquren='meiren' order by id limit 1");
////						if(count(json_decode($queryid))>0)
////						{
////							$id = json_decode($queryid)[0]->id;
////							$jiangpinstr="update lieren_weipiao set lingquren = '$openid',lqtime='$nowtime' where id='$id'";
////							$mysqlconnect->ExecuteData($jiangpinstr);
////							
////							$weipiao = json_decode($queryid)[0]->weipiao;
////							$jiangpinstr="update lieren_users set remark='$weipiao' where openid='$openid'";
////							$mysqlconnect->ExecuteData($jiangpinstr);
////							
////						}
////						else
////						{
////							$mysqlconnect->choujiang($mysqlconnect,$openid);
////							return;
////						}
////						
////						//根据上面的id改变此条信息的领取人
////					}
//					/////////////////////////////////////
//					$jieguo=11;
//					$jiangpinstr="update lieren_users set jiangpin1='1' where openid='$openid'";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr ="insert into lieren_wx_yemian values(null,'$openid','choujiang','jiangpin1','$nowtime')";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr="update lieren_jiangpin set shengyushu = shengyushu-1 where jiangpin=1";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					////////////////////////////////////////
//				}
//				else
//				{
//					$mysqlconnect->choujiang($mysqlconnect,$openid);
//					return;
//				}
//			}
//		}
//		if($gailv>11000 && $gailv<=19000)
//		{
//			if($user_jp2 == "1"){//中过该奖品
//				$jieguo=22;
//			}
//			if($user_jp2 == "2"){//此次中该奖品
//				if($shengyu_jp2>0)
//				{
//					$jieguo=21;
//					$jiangpinstr="update lieren_users set jiangpin2='1' where openid='$openid'";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr="insert into lieren_wx_yemian values(null,'$openid','choujiang','jiangpin2','$nowtime')";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr="update lieren_jiangpin set shengyushu = shengyushu-1 where jiangpin=2";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//				}
//				else
//				{
//					$mysqlconnect->choujiang($mysqlconnect,$openid);
//					return;
//				}
//			}
//		}
//		if($gailv>19000 && $gailv<=19040)
//		{
//			if($user_jp3 == "1"){//中过该奖品
//				$jieguo=32;
//			}
//			if($user_jp3 == "2"){//此次中该奖品
//				if($shengyu_jp3>0)
//				{
//					$jieguo=31;
//					$jiangpinstr="update lieren_users set jiangpin3='1' where openid='$openid'";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr="insert into lieren_wx_yemian values(null,'$openid','choujiang','jiangpin3','$nowtime')";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr="update lieren_jiangpin set shengyushu = shengyushu-1 where jiangpin=3";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//				}
//				else
//				{
//					$mysqlconnect->choujiang($mysqlconnect,$openid);
//					return;
//				}
//			}
//		}
//		if($gailv>19040 && $gailv<=19050)
//		{
//			if($user_jp4 == "1"){//中过该奖品
//				$jieguo=42;
//			}
//			if($user_jp4 == "2"){//此次中该奖品
//				if($shengyu_jp4>0)
//				{
//					$jieguo=41;
//					$jiangpinstr="update lieren_users set jiangpin4='1' where openid='$openid'";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr="insert into lieren_wx_yemian values(null,'$openid','choujiang','jiangpin4','$nowtime')";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr="update lieren_jiangpin set shengyushu = shengyushu-1 where jiangpin=4;";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//				}
//				else
//				{
//					$mysqlconnect->choujiang($mysqlconnect,$openid);
//					return;
//				}
//			}
//		}
//		if($gailv==190510000)
//		{
//			if($user_jp5 == "1"){//中过该奖品
//				$jieguo=52;
//			}
//			if($user_jp5 == "2"){//此次中该奖品
//				if($shengyu_jp5>0)
//				{
//					$jieguo=51;
//					$jiangpinstr="update lieren_users set jiangpin5='1' where openid='$openid'";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr="insert into lieren_wx_yemian values(null,'$openid','choujiang','jiangpin5','$nowtime')";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr="update lieren_jiangpin set shengyushu = shengyushu-1 where jiangpin=5;";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//				}
//				else
//				{
//					$mysqlconnect->choujiang($mysqlconnect,$openid);
//					return;
//				}
//			}
//		}
//		if($gailv>0 && $gailv<=10000)
//		{
//			if($user_jp6 == "1"){//中过该奖品
//				$jieguo=62;
//			}
//			if($user_jp6 == "2"){//此次中该奖品
//				if($shengyu_jp6>0)
//				{
//					$jieguo=61;
//					$jiangpinstr="update lieren_users set jiangpin6='1' where openid='$openid'";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr="insert into lieren_wx_yemian values(null,'$openid','choujiang','jiangpin6','$nowtime')";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//					$jiangpinstr="update lieren_jiangpin set shengyushu = shengyushu-1 where jiangpin=6;";
//					$mysqlconnect->ExecuteData($jiangpinstr);
//				}
//				else
//				{
//					$mysqlconnect->choujiang($mysqlconnect,$openid);
//					return;
//				}
//			}
//		}
//		if($gailv == 0)
//		{
//			$jieguo = 0;
//		}
//	//	echo date('y-m-d->h:i:s',time());
////		echo $jiangpinstr;
//		echo $jieguo;
//	}	

}
?>